package com.gitee.huanminabc.dynamicdb.dal.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gitee.huanminabc.dynamicdb.dal.mapper.TenantMapper;
import com.gitee.huanminabc.dynamicdb.entity.TenantEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Author mac
 * @Date 2024-12-11 10:22:37
 */
@Service
public class TenantManager extends ServiceImpl<TenantMapper, TenantEntity> implements IService<TenantEntity> {


    public List<TenantEntity> getTenants() {
        LambdaQueryWrapper<TenantEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TenantEntity::getState, 1);
        return list(queryWrapper);
    }



    /**
     * 分页查询
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    public   IPage<TenantEntity> page(TenantEntity entity, long pageNo, long pageSize) {
       return page(entity,pageNo,pageSize,true);
    }
    /**
     * 分页查询 不统计总数,一般用于导出数据的时候使用
     * @param entity
     * @param pageNo
     * @param pageSize
     * @return
     */
    public   IPage<TenantEntity> pageNotCount(TenantEntity entity, long pageNo, long pageSize) {
         return page(entity,pageNo,pageSize,false);
    }
    
    private IPage<TenantEntity> page(TenantEntity entity, long pageNo, long pageSize,boolean isSearchCount) {
        Page<TenantEntity> page = new Page<>(pageNo, pageSize,isSearchCount);
        LambdaQueryWrapper<TenantEntity> wrapper = getWrapperQuery(entity);
        return this.baseMapper.selectPage(page, wrapper);
    }


    /**
      * 条件查询
      * @param entity
      * @return
      */
    public List<TenantEntity> query(TenantEntity entity) {
        LambdaQueryWrapper<TenantEntity> wrapper = getWrapperQuery(entity);
        return this.baseMapper.selectList( wrapper);
    }


    /**
      * 查询条件
      * @param entity
      * @return
      */
    public LambdaQueryWrapper<TenantEntity> getWrapperQuery(TenantEntity entity) {
        return new QueryWrapper<TenantEntity>().lambda()
				.eq(Objects.nonNull(entity.getId()), TenantEntity::getId, entity.getId())
				.eq(StringUtils.isNotEmpty(entity.getTenantKey()), TenantEntity::getTenantKey, entity.getTenantKey())
				.eq(StringUtils.isNotEmpty(entity.getDatasourceKey()), TenantEntity::getDatasourceKey, entity.getDatasourceKey())
				.eq(StringUtils.isNotEmpty(entity.getDatasourceAsKey()), TenantEntity::getDatasourceAsKey, entity.getDatasourceAsKey())
				.eq(StringUtils.isNotEmpty(entity.getUpdateBy()), TenantEntity::getUpdateBy, entity.getUpdateBy())
				.eq(StringUtils.isNotEmpty(entity.getCreateBy()), TenantEntity::getCreateBy, entity.getCreateBy())
				.eq(StringUtils.isNotEmpty(entity.getRemark()), TenantEntity::getRemark, entity.getRemark())
				.orderByDesc(TenantEntity::getCreateTime);
    }
}